package com.yyx.demo.publishSubscribe.test;

import com.yyx.demo.lock.test.LockTest;
import com.yyx.demo.publishSubscribe.PublishSubscriber;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class testSubscriber2 {

    final static Logger log = LoggerFactory.getLogger(testSubscriber2.class);

    public static final String zkServerPath = "10.2.132.171:2181";
    public static final Integer timeout = 50000;

    private ZooKeeper createClient() throws IOException {
        ZooKeeper zk = new ZooKeeper(zkServerPath, timeout, new LockTest());
        return zk;
    }

    public static void main(String[] args) {
        testSubscriber2 testPublisher=new testSubscriber2();
        try {
            PublishSubscriber publishSubscriber=new PublishSubscriber(testPublisher.createClient(),null);
            while (true){
                byte[] bytes=publishSubscriber.SubscribeEvent("/test");
                System.out.println("接收到："+new String(bytes));
                try {
                    System.out.println("阻塞10s");
                    Thread.sleep(15*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
}
